package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.Menu;
import org.example.query.MenuQuery;
import org.example.service.impl.MenuServiceImpl;
import org.example.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-19
 */
@Api(description = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController<Menu,MenuQuery> {
    @Autowired
    private MenuServiceImpl menuServiceImpl;

    @ApiOperation(value="查询树结构菜单-联查")
    @PostMapping("/tree")
    public ResultBean getMenuServiceImpl(@RequestBody MenuQuery menuQuery) {
        List<Menu> tree=menuServiceImpl.tree(menuQuery.getQueryWrapper());
        return ResultBean.SUCCESS(tree);
    }
    //查询一级菜单
    @ApiOperation(value="查询一级菜单")
    @GetMapping("/level1")
    public ResultBean getLevel1Menu() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("parent_id");
        List<Menu> menus = menuServiceImpl.list(queryWrapper);
        return ResultBean.SUCCESS(menus);
    }
    @ApiOperation(value="查询所有菜单-单表")
    @GetMapping("/all/tree")
    public ResultBean allSingleMenu() {
        List<Menu> list=menuServiceImpl.tree(null);
        return ResultBean.SUCCESS(list);
    }
    @ApiOperation(value="删除菜单")
    @PostMapping("/delete")
    public ResultBean deleteMenu(@RequestBody Menu menu) {
        //这里用menu来进行接受数据
        //参数检验
        if(menu==null || menu.getId()==null) {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        //这里就不用那个查询的语句了 ，因为这里直接就是删除的功能
        boolean removed = menuServiceImpl.removeById(menu.getId());
        return removed?ResultBean.SUCCESS():ResultBean.ERROR("删除失败");
    }
    @ApiOperation(value = "批量删除" )
    @PostMapping("/deleteBatch/{ids}")
    public ResultBean getBatchDel(@PathVariable("ids") List<Integer> list) throws Exception {
        //因为在前端的时候，直接变成19，21的形式了，在后端只需要进行转换即可
        boolean flg=menuServiceImpl.removeByIds(list);
        if(!flg){
            throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
        }
        return ResultBean.SUCCESS();
    }
}
